#include "loginui.h"
#include "dcuinformation.h"
#include <QPushButton>
#include <QDebug>

LoginUI::LoginUI(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setAutoFillBackground(true);
    QPalette background = palette();
    background.setBrush(QPalette::Window, QBrush(QPixmap(":/images/defaultUI.png")));
    setPalette(background);

    QGroupBox *input = new QGroupBox(this);
    input->setGeometry(40, 30, 340, 380);
    input->setStyleSheet("QGroupBox{background:gray}");

    num = new QGroupBox(this);
    num->setGeometry(390, 30, 370, 380);
    num->setStyleSheet("QGroupBox{background:gray}");

    QLabel *text = new QLabel(input);
    text->setFont(QFont(ZITI, 15, QFont::Normal));
    text->setGeometry(QRect(5, 5, 331, 200));
    text->setStyleSheet("color:black;background:white");

    QLabel *textLabel = new QLabel(input);
    textLabel->setText("密 码：");
    textLabel->setFont(QFont(ZITI, 20, QFont::Normal));
    textLabel->setGeometry(QRect(5, 250, 80, 40));

    password = new QLabel(input);
    password->setFont(QFont(ZITI, 15, QFont::Normal));
    password->setGeometry(QRect(85, 250, 250, 40));
    password->setStyleSheet("color:black;background:white");

    QPushButton *loginBtn = new QPushButton(input);
    loginBtn->setText("登  陆");
    loginBtn->setFont(QFont(ZITI, 20, QFont::Normal));
    loginBtn->setGeometry(QRect(5, 330, 331, 40));
    connect(loginBtn, SIGNAL(clicked()), this, SLOT(loginBtnDone()));

    initNumUI();
}

void LoginUI::initNumUI()
{
    int total = 12;

    QStringList textList;
    textList << "9"
             << "8"
             << "7"
             << "4"
             << "5"
             << "6"
             << "1"
             << "2"
             << "3"
             << "0"
             << "退格"
             << "清空";

    for (int var = 0; var < total; ++var)
    {
        QPushButton *btn = new QPushButton(num);
        btn->setText(textList.at(var));
        btn->setFont(QFont(ZITI, 18, QFont::Normal));
        btn->setGeometry(QRect(5 + (21 + 100) * (var % 3), 5 + (50 + 50) * (var / 3), 120, 70));

        connect(btn, SIGNAL(clicked()), this, SLOT(numBtnDone()));
    }
}

void LoginUI::numBtnDone()
{
    QObject *    obj = QObject::sender();
    QPushButton *btn = qobject_cast<QPushButton *>(obj);

    if (btn->text() == "退格")
    {
        password->setText(password->text().mid(0, password->text().size() - 1));
    }
    else if (btn->text() == "清空")
    {
        password->setText("");
    }
    else
    {
        password->setText(password->text() + btn->text());
    }
}

void LoginUI::loginBtnDone()
{
    if (password->text() == "1")
    {
        emit pageInfo("登陆成功");
        password->setText("");
    }
    else
    {
        password->setText("密码错误");
    }
}
